﻿using System;
using System.Windows;
using System.Windows.Forms;
using System.Windows.Input;
using System.Windows.Media.Imaging;
using ImageTagToolViewModel.Interfaces;
using ImageTagToolViewModel.ViewModels;

namespace ImageTagTool
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window, IMainWindow
    {
        private MainWindowViewModel _ViewModel;

        public MainWindow()
        {
            InitializeComponent();
            _ViewModel = new MainWindowViewModel(this);
            DataContext = _ViewModel;
        }

        public void CloseWindow()
        {
            Close();
        }

        public void ShowAboutBox()
        {
            new About().ShowDialog();
        }


        public void ShowPicture(string path)
        {
            // Loading the image.
            BitmapImage image = new BitmapImage();
            image.BeginInit();
            image.CacheOption = BitmapCacheOption.OnLoad;
            image.UriSource = new Uri(path, UriKind.RelativeOrAbsolute);
            image.EndInit();
            MainImage.Source = image;
            ZoomBorder.Reset();
        }


        public string OpenFolder()
        {
            FolderBrowserDialog dialog = new FolderBrowserDialog();
            dialog.ShowDialog();
            return dialog.SelectedPath;
        }


        public void ShowMessageBox(string message)
        {
            System.Windows.MessageBox.Show(message);
        }

        private void TagTextBox_PreviewKeyDown(object sender, System.Windows.Input.KeyEventArgs e)
        {
            _ViewModel.TagTextBox_PreviewKeyDown(e.Key);
        }

        /// <summary>
        /// Adds the selected tag to image-tags by double clicking on it.
        /// </summary>
        private void DBTagsListBox_PreviewMouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            if (DBTagsListBox.SelectedIndex > -1)
            {
                string tag = DBTagsListBox.SelectedItem as string;
                if (!_ViewModel.ImageTagsList.Contains(tag))
                    _ViewModel.ImageTagsList.Add(tag);
            }
        }

        /// <summary>
        /// Removes the selected tag from the image.
        /// </summary>
        private void TagListBox_PreviewMouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            if (TagListBox.SelectedIndex > -1)
                _ViewModel.RemoveTagFromImage(TagListBox.SelectedItem as string);
        }

        /// <summary>
        /// Removes the selected tag from the database.
        /// </summary>
        private void RemoveTagContextMenuItem_Click(object sender, RoutedEventArgs e)
        {
            if (DBTagsListBox.SelectedIndex > -1)
                _ViewModel.RemoveTagFromDatabase(DBTagsListBox.SelectedItem as string);
        }

        public void ResizePicture(int width, int height)
        {
            ZoomBorder.Resize(width, height);
        }
    }
}
